﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SalesPortal.Web.HttpHandlers
{
    /// <summary>
    /// Reference: http://www.hanselman.com/blog/BackToBasicsDynamicImageGenerationASPNETControllersRoutingIHttpHandlersAndRunAllManagedModulesForAllRequests.aspx
    /// 
    /// You will need to configure this handler in the Web.config file of your 
    /// web and register it with IIS before being able to use it. For more information
    /// see the following link: http://go.microsoft.com/?linkid=8101007
    /// </summary>
    public class CustomWebControlHandler : IHttpHandler
    {
        // Return false in case your Managed Handler cannot be reused for another request.
        // Usually this would be false in case you have some state information preserved per request.
        public bool IsReusable { get { return false; } }

        protected RequestContext RequestContext { get; set; }

        public CustomWebControlHandler() : base() { }

        public CustomWebControlHandler(RequestContext requestContext)
        {
            this.RequestContext = requestContext;
        }

        public void ProcessRequest(HttpContext context)
        {
            // build up the control hiearchy - a Panel as the root, with
            // two Labels and a LiteralControl as children...
            Panel p = new Panel();

            Label lbl1 = new Label();
            lbl1.Text = "Hello, World!";
            lbl1.Font.Bold = true;

            Label lbl2 = new Label();
            lbl2.Text = "How are you?";
            lbl2.Font.Italic = true;

            p.Controls.Add(lbl1);
            p.Controls.Add(new LiteralControl(" - "));
            p.Controls.Add(lbl2);

            // Render the Panel control
            StringWriter sw = new StringWriter();
            HtmlTextWriter writer = new HtmlTextWriter(sw);
            p.RenderControl(writer);

            // Emit the rendered HTML
            context.Response.Write(sw.ToString());
        }
    }
}